<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use think\Exception;
use think\Log;
use AstroTool\Cache\CacheManage;

class ZlAdminVip extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_admin_vip';

    protected $cacheModel = 'config.admin_vip';

    /**
     * 获取内部VIP用户
     * @param $level
     * @return array
     * @throws \Exception
     */
    public function getVipInfo($level)
    {
        $vipData = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $level)->getDataAndSet(function () use ($level)
        {
            try
            {
                $uids = $this->where(['status' => 1, 'type' => $level])->column('uid');

                $data = [];
                foreach ($uids as $id)
                {
                    $data[$id] = $id;
                }
            }
            catch (Exception $e)
            {
                Log::write('获取内部VIP' . $level . '错误' . $e->getMessage(), 'error');
                $data = [];
            }

            return $data ?: [''];
        });

        if (!empty($vipData))
        {
            $vipData = array_values($vipData);
        }

        return array_map(function ($val)
        {
            if ($val)
            {
                return (int)$val;
            }
        }, $vipData);
    }
}
